Page({
  data: {
    tabbar: [
      '推荐分类',
      '进口超市',
      '国际名牌',
      '奢侈品',
      '海囤全球',
      '男装',
      '女装',
      '男鞋',
      '女鞋',
      '钟表珠宝',
      '手机数码',
      '电脑办公',
      '家用电器',
      '玩具乐器',
      '运动户外',
      '宠物生活',
      '特产馆'
    ],
    height: 0, //scroll-view高度
    top: 0,
    currentTab: 0, //预设当前项的值
    scrollView_leftId: 'left_0',
    scrollView_rightId: 'right_0',
    scrollTop: 0,
    distanceTop: 44,
    isScroll: true,
    isTap: true
  },
  onLoad: function (options) {
    setTimeout(() => {
      wx.getSystemInfo({
        success: res => {
          this.setData({
            height: res.windowHeight - res.windowWidth / 750 * 92,
            distanceTop:res.windowWidth / 750 * 92
          })
        }
      });
    }, 50);
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    let cur = e.currentTarget.dataset.current;
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      }, () => {
        this.checkCor();
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function (isScroll) {
    if (!isScroll) {
      this.data.isScroll = false;
      this.setData({
        isTap: true
      })
      if (this.data.currentTab > 6) {
        this.setData({
          scrollView_leftId: `left_${this.data.currentTab - 2}`
        })
      } else {
        this.setData({
          scrollView_leftId: 'left_0'
        })
      }
      this.setData({
        scrollView_rightId: `right_${this.data.currentTab}`
      })
    } else {
      this.setData({
        scrollView_leftId: `left_${this.data.currentTab}`
      })
    }
  },
  productList(e) {
    let key = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/template/mall/productList/productList?searchKey=' + key
    });
  },
  search: function () {
    wx.navigateTo({
      url: '/pages/template/news/search/search'
    });
  },
  scroll(e) {
    //动画时长固定300ms
    if (!this.data.isScroll) {
      setTimeout(() => {
        this.data.isScroll = true;
      }, 150);
    } else {
      this.setData({
        scrollTop: e.detail.scrollTop
      })
    }
  },
  linkage(e) {
    if (e.detail.isLinkage && e.detail.index != this.data.currentTab) {
      this.setData({
        isTap:false,
        currentTab:e.detail.index
      },()=>{
        this.checkCor(true);
      })
    }
  }
})